package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
@CrossOrigin
public class AddressController {


    @Reference
    private AddressService addressService;

    /**
     * 查询地址列表
     *
     * @return
     */
    @GetMapping("findAll")
    public Result QueryPageaddress() {
        List<Address> address = addressService.queryPageaddress();
        if (address != null) {
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, address);
        }
        return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);

    }


    /**
     * 新增分公司
     *
     * @param address
     * @return
     */
    @PostMapping("submit")
    public Result addAddress(@RequestBody Address address) {
        try {
            addressService.addAddress(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    /**
     * 弹出修改框数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result ShowUpdateAddress(@PathVariable Integer id) {
        Address address = addressService.queryById(id);
        if (address != null) {
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, address);
        }
        return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
    }


    /**
     * 修改地址
     *
     * @param address
     * @return
     */
    @PutMapping("/update")
    public Result updateAddress(@RequestBody Address address) {
        try {
            addressService.updateAddress(address);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteAddress(@PathVariable Integer id) {
        try {
            addressService.deleteAddress(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }


    }

    /**
     * 是否启用按钮操作
     * @param id
     * @return
     */
    @PutMapping("/enable/{id}")
    public Result dom_Storage(@PathVariable Integer id){
        try {
            addressService.dom_Storage(id);
            return new Result(true,MessageConstant.ADDRESSFAMILY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADDRESSFAMILY_FAIL);
        }
    }
}
